#region Copyright RenGuiYou. All rights reserved.
//=====================================================
// PandoraFrameWork
// Author:      RenGuiyou
// Feedback: 	mailto:750539605@qq.com
//=====================================================
#endregion

using System;
using System.Collections.Generic;
using System.Reflection;
using Pandora;
using UnityEditor;
using UnityEngine;

// ReSharper disable UnusedMember.Local

namespace PandoraEditor.Window
{
    public class PdrWindow : EditorWindow
    {
        private List<string> m_ToolBarTitle = new List<string>();
        private List<IDrawPanel> m_ToolBarAction = new List<IDrawPanel>();
        private static PdrWindow m_Window;
        public static PdrWindow PandoraWindow
        {
            get
            {
                return m_Window;
            }
        }
        private int selectToolbarId;
        private string[] m_TopTitles;

        public static void Open()
        {
            m_Window = GetWindow<PdrWindow>();
            m_Window.Show();
        }

        public new static T GetWindow<T>() where T : PdrWindow
        {
            PdrWindow pdrWindow = GetWindow<T>("Pandora");
            pdrWindow.AddBaseToolAction();
            return (T)pdrWindow;
        }

        public void AddToolBar(string toolTitle, Type t)
        {
            ConstructorInfo constructor = t.GetConstructor(new Type[0]);
            if (constructor == null) Debug.LogError("[扩展模块]构造函数不能为私有类型...");
            var obj = constructor.Invoke(new object[0]);
            AddToolBar(toolTitle, obj as IDrawPanel);
        }

        public void AddToolBar(string toolTitle, IDrawPanel drawPanel)
        {
            m_ToolBarTitle.Add(toolTitle);
            m_ToolBarAction.Add(drawPanel);
            m_TopTitles = m_ToolBarTitle.ToArray();
        }


        private void AddBaseToolAction()
        {
            if (m_ToolBarTitle.Count == 0)
            {
                AddToolBar("贴图检测", typeof(CheckTexture));
                AddToolBar("全局检测", typeof(GlobalExpress));
                AddToolBar("查找组件", typeof(FindComponent));
                var list = PdrAssemblyHelper.GetAttributeClass<PdrWindowAbbr>();
                for (int i = 0; i < list.Count; i++)
                {
                    AddToolBar(list[i].attribute.name, Activator.CreateInstance(list[i].type) as IDrawPanel);
                }
            }
        }

        public void OnInspectorUpdate()
        {
            Repaint();
        }

        public void OnGUI()
        {
            if (m_ToolBarAction.Count == 0)
                return;
            GUILayout.Space(10);
            int fontsize = GUI.skin.label.fontSize;
            GUI.skin.label.fontSize = 24;
            TextAnchor anchor = GUI.skin.label.alignment;
            GUI.skin.label.alignment = TextAnchor.MiddleCenter;
            GUILayout.Label(m_TopTitles[selectToolbarId]);
            GUILayout.Space(10);
            selectToolbarId = GUILayout.Toolbar(selectToolbarId, m_TopTitles);
            IDrawPanel panel = m_ToolBarAction[selectToolbarId];
            panel.DrawPanel();
            GUI.skin.label.fontSize = fontsize;
            GUI.skin.label.alignment = anchor;
        }
    }
}